<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <base href="${request.contextPath}/"/>
    <meta charset="UTF-8">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,minimum-scale=1.0">
    <title>收货地址</title>
    <link href="Content/wechat/css/reset.css" rel="stylesheet" type="text/css">
    <!--签到-->
    <link href="Content/wechat/css/dangJian_gongZhuGongJian_CJHD.css" rel="stylesheet" type="text/css">


    <script src="Content/wechat/js/jquery.min.js"></script>
    <!--字体缩放-->
    <script src="Content/wechat/js/ziti.js"></script>
    <!--end 字体缩放-->
    <!--上传图片js-->
    <script src="Content/wechat/js/svg.js"></script>
    <!--end 上传图片js-->
    <script src="Content/wechat/js/jquery.validate.js"></script>
</head>
<body>
<!--<header class="clearfix">-->
<!--    <h2>收货地址</h2><button class="quxiao_btn">取消</button>-->
<!--</header>-->
<form class="xinyuan" id="form1">
    <input type="hidden" name="id" id="id" value="${id!}"/>
    <div class="xinyuan_box">

        <dl class="clearfix">
            <dt>姓名</dt>
            <dd>
                <input class="text" type="text" name="name" placeholder="请输入姓名">
            </dd>
        </dl>


        <dl class="clearfix">
            <dt>联系电话</dt>
            <dd>
                <input class="text" type="text" name="phone" placeholder="请输入联系电话">
            </dd>
        </dl>
        <dl class="clearfix">
            <dt>省份</dt>
            <dd><select name="provinceid" id="provinceid" onchange="provinceChange(this.value)">
                <option value="">---请选择---</option>
            </select>
            </dd>
        </dl>
        <dl class="clearfix">
            <dt>城市</dt>
            <dd><select name="cityid" id="cityid" onchange="cityChange(this.value)">
                <option value="">---请选择---</option>
            </select>
            </dd>
        </dl>
        <dl class="clearfix">
            <dt>区</dt>
            <dd><select name="regionid" id="regionid">
                <option value="">---请选择---</option>

            </select>
            </dd>
        </dl>
        <dl class="clearfix">
            <dt>具体位置</dt>
            <dd>
                <input class="text" type="text" name="info" id="info" placeholder="请输入具体位置">
            </dd>
        </dl>
        <dl>
            <dt><input type="checkbox" name="isdef" id="isdef" value="1" style="margin-right: 10px;">设置默认</dt>
        </dl>

    </div>
</form>
<div class="button_shangChuan" id="btnAdd">确定</div>
<div class="button_shangChuan" id="btnDel" style="display:none; " onclick="btnDel()">删除</div>
</div>
</body>
<script>
    var provinceList =${ProvinceList!};
    var cityList =${CityList!};
    var pegionList =${RegionList!};
    $.each(provinceList, function (index, items) {
        $("#provinceid").append("<option value='" + items.id + "'>" + items.provincename + "</option>");
    });

    function provinceChange(provinceid) {
        $("#cityid").empty();
        $("#cityid").append("<option value=\"\">---请选择---</option>");
        $.each(cityList, function (index, items) {
            if (items.provinceid == provinceid) {
                $("#cityid").append("<option value='" + items.id + "'>" + items.cityname + "</option>");
            }
        });

    }


    function cityChange(cityid) {
        $("#regionid").empty();
        $("#regionid").append("<option value=\"\">---请选择---</option>");
        $.each(pegionList, function (index, items) {
            if (items.cityid == cityid) {
                $("#regionid").append("<option value='" + items.id + "'>" + items.regionname + "</option>");
            }
        });

    }

    function btnDel() {
        $.ajax({
            //几个参数需要注意一下
            type: "POST",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "/wechat/personage/set/address/Del",//url
            data: $('#form1').serialize(),
            success: function (result) {
                console.log(result);//打印服务端返回的数据(调试用)
                if (result.code == 200) {
                    alert(result.info);
                    // location.reload();
                    window.location = " /wechat/personage/set/address/Index";
                } else {
                    alert("请稍后再试");
                }
            },
            error: function () {
                alert("异常！");
            }
        });

    }

    var btnAdd = true;
    $().ready(function () {
        $("#btnAdd").click(function () {
                if (!btnAdd) {
                    alert("添加中...");
                    return;
                }
                if ($("#form1").valid()) {
                    btnAdd = false;
                    $("#btnAdd").html("添加中...")
                    $.ajax({
                        //几个参数需要注意一下
                        type: "POST",//方法类型
                        dataType: "json",//预期服务器返回的数据类型
                        url: "/wechat/personage/set/address/Form",//url
                        data: $('#form1').serialize(),
                        success: function (result) {
                            console.log(result);//打印服务端返回的数据(调试用)
                            if (result.code == 200) {
                                alert(result.info);
                                // location.reload();
                                window.location = " /wechat/personage/set/address/Index";
                            } else {
                                alert("请稍后再试");
                                btnAdd = true;
                                $("#btnAdd").html("确定");
                            }
                        },
                        error: function () {
                            alert("异常！");
                            btnAdd = true;
                            $("#btnAdd").html("确定");
                        }
                    });
                }
            }
        );
        $("#form1").validate({
            debug: true,
            rules: {
                name: {
                    required: true,
                    minlength: 2,
                    maxlength: 5,

                },
                phone: {
                    required: true,
                    isPhoneNum: true
                }
                ,
                regionid: {
                    required: true,

                }
                ,
                info: {
                    required: true,

                }

            },
            messages: {
                name: {
                    minlength: "(字符不能少于 2 个且不能大于 5个)",
                    maxlength: "(字符不能少于 2 个且不能大于 5个)",
                },

            }


        })
        jQuery.validator.addMethod("isPhoneNum", function (value, element) {
            var length = value.length;
            var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
            return this.optional(element) || (length == 11 && mobile.test(value));
        }, "请正确填写您的手机号码");


        var json = null;
        if ($("#id").val() != null && $("#id").val() != "") {
            $("#btnDel").show();
            json =${shaddress!};
            $("input[name='name']").val(json.name);
            $("input[name='phone']").val(json.phone);
            $("input[name='info']").val(json.info);
            $("#provinceid").val(json.provinceid);
            provinceChange(json.provinceid);
            $("#cityid").val(json.cityid);
            cityChange(json.cityid);
            $("#regionid").val(json.regionid);
            $("#isdef[value='" + json.isdef + "']").attr('checked', 'true');
        }
    });

</script>
</html>